import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询总代备件进销存'
});

const exportPartStockDesc = formatMessage({
    id: 'api.exportPartStockDesc',
    defaultMessage: '导出总代备件进销存'
});

const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代'
});

const importTempDesc = formatMessage({
    id: 'api.importTempDesc',
    defaultMessage: '下载导入模板'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/partStock/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询总代备件进销存
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/partStocks/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 导出
export const exportPartStocks = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partStocks/export/forCurrentUser?${queryString}`, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportPartStockDesc))
        .then(res => res.data);
};

// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};

export const partStockImportTemp = () => request(`${baseUrl}/partStocks/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
